package com.example.demo.servlet;

import com.example.demo.service.NovelService;
import com.example.demo.service.impl.NovelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author wan
 * @version 1.0.0
 * @Description 处理前端页面跳转
 * @createTime 2021年08月20日 11:26:27
 */
@Slf4j
@WebServlet("/HelloNovel")
public class HelloNovel extends HttpServlet {

    NovelService novelService = NovelServiceImpl.getObject();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //对书库的访问进行处理
        String depository = request.getParameter("depository");
        if (StringUtils.isNotBlank(depository)) {
            depositoryDeal(depository, request, response);
        }
        //对搜索的访问进行处理
        String search = request.getParameter("search");
        if (StringUtils.isBlank(search) && StringUtils.isNotBlank(depository)) {
            search = depository;
        }
        //只要搜索就清空一切 session
        request.getSession().invalidate();
        HttpSession session = request.getSession();
        if (StringUtils.isBlank(search)) {
            session.setAttribute("path", "请不要输入空白,请重新搜索：");
            request.getRequestDispatcher("/find.jsp").forward(request, response);
        }
        //校验本地或网络是否存在该小说
        String verification = novelService.novelVerification(search);
        switch (verification) {
            case NovelServiceImpl.NONE:
                session.setAttribute("path", "小说未找到!");
                request.getRequestDispatcher("/find.jsp").forward(request, response);
                break;
            case NovelServiceImpl.LOCAL:
                break;
            default:
                //记录该书在网络的url
                request.setAttribute("internet", verification);
                break;
        }
        setPageNum(request, search);
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        String novelName = (String) session.getAttribute("novelName");
        int pageNum = (int) session.getAttribute("pageNum");
        String next = (String) request.getParameter("next");
        String front = (String) request.getParameter("front");
        if ((null == next && null == front) || "下一页".equals(next)) {
            session.setAttribute("pageNum", pageNum + 1);
        } else {
            session.setAttribute("pageNum", pageNum - 1);
            pageNum -= 2;
            if (pageNum == 0) {
                pageNum = 1;
            }
        }
        //看看请求方向：本地？网络？
        String internet = (String) request.getAttribute("internet");
        List<String> list = null;
        if (StringUtils.isNotBlank(internet)) {
            list = novelService.getNovelFromInternet(novelName, (String) request.getAttribute("internet"), pageNum, "/software/novel/" + novelName + ".txt");
        } else {
            list = novelService.getNovel(novelName, pageNum);
        }
        request.setAttribute("novels", list);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * 通过session设置参数
     *
     * @param request
     * @param search
     */
    private void setPageNum(HttpServletRequest request, String search) {
        request.getSession().setAttribute("novelName", search);
        if (null == request.getSession().getAttribute("pageNum")) {
            request.getSession().setAttribute("pageNum", 1);
        }
    }

    /**
     * -- 校验本地数据库是否存在
     *
     * @param depository
     * @param request
     * @param response
     */
    public void depositoryDeal(String depository, HttpServletRequest request, HttpServletResponse response) {
        if (!NovelServiceImpl.pathCheck(depository)) {
            try {
                request.getRequestDispatcher("between.html").forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
